class Popup_Text
  attr_accessor   :text
  attr_accessor   :life_timer
  attr_accessor   :left_life_timer
  attr_accessor   :delay_timer
  attr_accessor   :_sprite
  attr_accessor   :_duration
  attr_reader   :res_font_size
  attr_accessor   :start_font_size
  attr_accessor   :_font_color
  attr_accessor   :animation_timer
  attr_accessor   :drawed
  attr_accessor   :cur_size
  attr_accessor   :opacity
  def initialize(text, type)
    @text = text
    @life_timer = 40
    @left_life_timer = @life_timer
    @animation_timer = 0
    @drawed = false
    @opacity= 255
    @delay_timer = 0
    case type
      when 2 # 暴击
        @cur_size = 36 + rand(4)
        @_font_color = Color.new(255,0,0)
      when 3 # 回血
        @cur_size = 30 + rand(4)
        @_font_color = Color.new(251,245,66)
      else
        @cur_size = 32 + rand(4)
        @_font_color = Color.new(255,255,255)
    end
  end
  def draw(x, y, oy, viewport)
    bitmap = Bitmap.new(160, 48)
    bitmap.font.name = "Arial Black"
    bitmap.font.color.set(@_font_color.red, @_font_color.green, @_font_color.blue)
    bitmap.font.size = @cur_size
    @_sprite = ::Sprite.new(viewport)
    @_sprite.bitmap = bitmap
    @_sprite.ox = 80
    @_sprite.oy = 20
    @_sprite.x = x
    @_sprite.y = y - oy / 2
    @_sprite.z = 3000
    @_duration = 40
    @drawed = true
  end
  def draw_text
    bitmap = @_sprite.bitmap
    bitmap.clear
    bitmap.font.size = @cur_size.to_i
    bitmap.font.color.set(0, 0, 0, @opacity)
    bitmap.draw_text(-1, 12-1, 160, 36, @text, 1)
    bitmap.draw_text(+1, 12-1, 160, 36, @text, 1)
    bitmap.draw_text(-1, 12+1, 160, 36, @text, 1)
    bitmap.draw_text(+1, 12+1, 160, 36, @text, 1)
    bitmap.font.color.set(@_font_color.red, @_font_color.green, @_font_color.blue, @opacity)
    bitmap.draw_text(0, 12, 160, 36, @text, 1)
  end
  def update
    if @delay_timer > 0
      @delay_timer -= 1
      return
    end
    @left_life_timer -= 1
    if @left_life_timer < 25
      @cur_size -= 1
      # @opacity -= 5
      if @cur_size <= 6
        @cur_size = 6
      end
      if @opacity <= 0
        @opacity = 0
      end
    end
    draw_text
    @_sprite.y -= 2
    if @left_life_timer == 0
      @_sprite.dispose
    end
  end
  def lerp(val1, val2, t)
    return val1 + (val2 - val1) * t
  end
end
